/*
 * MainFrame.java
 *
 * Created on 29-Mar-2010, 00:33:33
 */
package gui;

import backend.Core;
import backend.Plugin;
import backend.RegexPlugin;
import backend.Server;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author Khazidhea
 */
public class MainFrame extends javax.swing.JFrame {

    private static Core core;
    private AddEventDialog addEventDialog;
    private EditEventDialog editEventDialog;
    private AddServerDialog addServerDialog;
    private AddLogDialog addLogDialog;
    private EditLogDialog editLogDialog;
    private DefaultListModel eventListModel = new DefaultListModel();
    private DefaultListModel serverListModel = new DefaultListModel();
    private DefaultListModel logListModel = new DefaultListModel();

    /** Creates new form MainFrame */
    public MainFrame() {

        initComponents();
        core = new Core();



        eventList.setModel(eventListModel);
        for (Plugin event : core.getEventList()) {
            eventListModel.addElement(event);
        }

        for (Server server : core.getServerList()) {
            serverListModel.addElement(server);
        }

        addEventDialog = new AddEventDialog(this);
        editEventDialog = new EditEventDialog(this);
        addServerDialog = new AddServerDialog(this, true);
        addLogDialog = new AddLogDialog(this, true);
        editLogDialog = new EditLogDialog(this, true);

        logList.setModel(logListModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTabPanel = new javax.swing.JTabbedPane();
        eventsPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        eventList = new javax.swing.JList();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        commandLabel = new javax.swing.JLabel();
        commandTextField = new javax.swing.JTextField();
        serversLabel = new javax.swing.JLabel();
        serversTextField = new javax.swing.JTextField();
        addEventButton = new javax.swing.JButton();
        editEventButton = new javax.swing.JButton();
        runEventButton = new javax.swing.JButton();
        eventsLabel = new javax.swing.JLabel();
        removeEventButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        serversPanel = new javax.swing.JPanel();
        nameLabel1 = new javax.swing.JLabel();
        serverNameTextField = new javax.swing.JTextField();
        ipTextField = new javax.swing.JTextField();
        ipLabel = new javax.swing.JLabel();
        serverListLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        serverList = new javax.swing.JList();
        logListLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        logList = new javax.swing.JList();
        addLogButton = new javax.swing.JButton();
        adServerButton = new javax.swing.JButton();
        removeServerButton = new javax.swing.JButton();
        removeLogButton = new javax.swing.JButton();
        saveChangesButton = new javax.swing.JButton();
        editLogButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DLF");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        eventList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        eventList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                eventListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(eventList);

        nameLabel.setText("Name:");

        nameTextField.setEditable(false);

        commandLabel.setText("Command:");

        commandTextField.setEditable(false);

        serversLabel.setText("Servers:");

        serversTextField.setEditable(false);

        addEventButton.setText("Add");
        addEventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addEventButtonActionPerformed(evt);
            }
        });

        editEventButton.setText("Edit");
        editEventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editEventButtonActionPerformed(evt);
            }
        });

        runEventButton.setText("Run");
        runEventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runEventButtonActionPerformed(evt);
            }
        });

        eventsLabel.setText("Events:");

        removeEventButton.setText("Remove");
        removeEventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeEventButtonActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName(""); // NOI18N
        jScrollPane4.setViewportView(jTextArea1);

        javax.swing.GroupLayout eventsPanelLayout = new javax.swing.GroupLayout(eventsPanel);
        eventsPanel.setLayout(eventsPanelLayout);
        eventsPanelLayout.setHorizontalGroup(
            eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eventsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(eventsLabel)
                    .addGroup(eventsPanelLayout.createSequentialGroup()
                        .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(eventsPanelLayout.createSequentialGroup()
                                .addComponent(addEventButton, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                                .addGap(49, 49, 49))
                            .addGroup(eventsPanelLayout.createSequentialGroup()
                                .addComponent(runEventButton, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                                .addGap(49, 49, 49))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, eventsPanelLayout.createSequentialGroup()
                                .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(removeEventButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                                    .addComponent(editEventButton, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE))
                                .addGap(49, 49, 49)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(eventsPanelLayout.createSequentialGroup()
                                .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(commandLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                                    .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(serversLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 30, Short.MAX_VALUE)
                                .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(nameTextField)
                                    .addComponent(serversTextField)
                                    .addComponent(commandTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        eventsPanelLayout.setVerticalGroup(
            eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eventsPanelLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(eventsLabel)
                .addGap(18, 18, 18)
                .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(eventsPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addComponent(runEventButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addEventButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editEventButton))
                    .addGroup(eventsPanelLayout.createSequentialGroup()
                        .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nameLabel)
                            .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(commandLabel)
                            .addComponent(commandTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(eventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(serversTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(serversLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeEventButton)
                .addGap(22, 22, 22))
        );

        mainTabPanel.addTab("Events", eventsPanel);

        nameLabel1.setText("Name:");

        serverNameTextField.setEnabled(false);

        ipTextField.setEnabled(false);

        ipLabel.setText("IP:");

        serverListLabel.setText("Server List:");

        serverList.setModel(serverListModel);
        serverList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        serverList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                serverListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(serverList);

        logListLabel.setText("Log List");

        logList.setEnabled(false);
        logList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                logListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(logList);

        addLogButton.setText("Add Log");
        addLogButton.setEnabled(false);
        addLogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLogButtonActionPerformed(evt);
            }
        });

        adServerButton.setText("Add Server");
        adServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adServerButtonActionPerformed(evt);
            }
        });

        removeServerButton.setText("Remove Server");
        removeServerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeServerButtonActionPerformed(evt);
            }
        });

        removeLogButton.setText("Remove Log");
        removeLogButton.setEnabled(false);
        removeLogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeLogButtonActionPerformed(evt);
            }
        });

        saveChangesButton.setText("Save Changes");
        saveChangesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveChangesButtonActionPerformed(evt);
            }
        });

        editLogButton.setText("Edit Log");
        editLogButton.setEnabled(false);
        editLogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editLogButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout serversPanelLayout = new javax.swing.GroupLayout(serversPanel);
        serversPanel.setLayout(serversPanelLayout);
        serversPanelLayout.setHorizontalGroup(
            serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serversPanelLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(serversPanelLayout.createSequentialGroup()
                        .addComponent(adServerButton, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                        .addGap(418, 418, 418))
                    .addGroup(serversPanelLayout.createSequentialGroup()
                        .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(serversPanelLayout.createSequentialGroup()
                                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                                    .addComponent(serverListLabel))
                                .addGap(67, 67, 67)
                                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(addLogButton, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                                    .addComponent(logListLabel)
                                    .addComponent(editLogButton, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                                    .addComponent(removeLogButton, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)))
                            .addGroup(serversPanelLayout.createSequentialGroup()
                                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ipLabel)
                                    .addComponent(nameLabel1))
                                .addGap(18, 18, 18)
                                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(ipTextField)
                                    .addComponent(serverNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE))))
                        .addGap(14, 14, 14))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, serversPanelLayout.createSequentialGroup()
                        .addComponent(removeServerButton, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                        .addGap(418, 418, 418))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, serversPanelLayout.createSequentialGroup()
                .addContainerGap(662, Short.MAX_VALUE)
                .addComponent(saveChangesButton)
                .addContainerGap())
        );
        serversPanelLayout.setVerticalGroup(
            serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serversPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel1)
                    .addComponent(serverNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ipLabel)
                    .addComponent(ipTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverListLabel)
                    .addComponent(logListLabel))
                .addGap(18, 18, 18)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(serversPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(addLogButton)))
                .addGap(11, 11, 11)
                .addGroup(serversPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(serversPanelLayout.createSequentialGroup()
                        .addComponent(adServerButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeServerButton))
                    .addGroup(serversPanelLayout.createSequentialGroup()
                        .addComponent(editLogButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeLogButton)))
                .addGap(30, 30, 30)
                .addComponent(saveChangesButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        mainTabPanel.addTab("Servers", serversPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 778, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        //Due to some bug not working right now. This method is not called when program closes.
    	//Because of that I have to save server and event lists after any modification made, rather than running them once in the end.
    	//Not really required right now.
    	System.out.println("ok");
        getCore().saveEventList();
        getCore().saveServerList();
        System.exit(0);
    }//GEN-LAST:event_formWindowClosed

    private void addEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEventButtonActionPerformed
        addEventDialog.setVisible(true);
    }//GEN-LAST:event_addEventButtonActionPerformed

    private void eventListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_eventListValueChanged

        if (eventList.getSelectedIndex() != -1) {
            Plugin event = (Plugin) eventListModel.get(eventList.getSelectedIndex());
            nameTextField.setText(event.getName());
            commandTextField.setText(event.getCommandStr());
            String servers = "";
            for (Server server : event.getServerList()) {
                servers += server.getName() + ", ";
            }
            servers = servers.substring(0, servers.length() - 2);
            serversTextField.setText(servers);

        }
    }//GEN-LAST:event_eventListValueChanged

    private void editEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editEventButtonActionPerformed
        if (eventList.getSelectedIndex() != -1) {
            Plugin event = (Plugin) eventListModel.get(eventList.getSelectedIndex());
            editEventDialog.setEvent(event);
            editEventDialog.setVisible(true);
        }
    }//GEN-LAST:event_editEventButtonActionPerformed

    private void removeEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeEventButtonActionPerformed
        eventListModel.remove(eventList.getSelectedIndex());
        core.saveEventList();
    }//GEN-LAST:event_removeEventButtonActionPerformed

    private void serverListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_serverListValueChanged
        //Show corresponding logs
    	if (serverList.getSelectedIndex() != -1) {
            serverNameTextField.setEnabled(true);
            ipTextField.setEnabled(true);
            logList.setEnabled(true);
            addLogButton.setEnabled(true);
            editLogButton.setEnabled(true);
            removeLogButton.setEnabled(true);

            Server server = (Server) serverListModel.get(serverList.getSelectedIndex());
            serverNameTextField.setText(server.getName());
            ipTextField.setText(server.getIp());

            getLogListModel().clear();
            for (String log : server.getLogs()) {
                getLogListModel().addElement(log);
            }
        } else {
            serverNameTextField.setEnabled(false);
            ipTextField.setEnabled(false);
            logList.setEnabled(false);
            addLogButton.setEnabled(false);
            editLogButton.setEnabled(false);
            removeLogButton.setEnabled(false);
        }
}//GEN-LAST:event_serverListValueChanged

    private void logListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_logListValueChanged
        if (evt.getValueIsAdjusting() == false) {
        }
}//GEN-LAST:event_logListValueChanged

    private void saveChangesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveChangesButtonActionPerformed
        Server server = (Server) serverListModel.get(serverList.getSelectedIndex());
        String oldName = server.getName();
        server.setName(serverNameTextField.getText());
        String oldIp = server.getIp();
        server.setIp(ipTextField.getText());

        ArrayList<String> logs = new ArrayList<String>();
        Enumeration e = getLogListModel().elements();
        while (e.hasMoreElements()) {
            String log = (String) e.nextElement();
            logs.add(log);
        }
        server.setLogs(logs);

        core.saveServerList();

        //renaming server in all the events
        // ADD renaming logs
        for (Plugin event : core.getEventList()) {
            for (Server serverFromEvent : event.getServerList()) {
                if (serverFromEvent.getName().equals(oldName)) {
                    serverFromEvent.setName(serverNameTextField.getText());
                }
                if (serverFromEvent.getIp().equals(oldIp)) {
                    serverFromEvent.setName(ipTextField.getText());
                }
            }
        }

        core.saveEventList();
}//GEN-LAST:event_saveChangesButtonActionPerformed

    private void adServerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adServerButtonActionPerformed
        addServerDialog.setVisible(true);
    }//GEN-LAST:event_adServerButtonActionPerformed

    private void removeServerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeServerButtonActionPerformed
        if (serverList.getSelectedIndex() != -1) {
            Server server = (Server) serverListModel.get(serverList.getSelectedIndex());
            //core.getServerList().remove(server);
            //need a warning and delete all connected events!
        }
    }//GEN-LAST:event_removeServerButtonActionPerformed

    private void addLogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLogButtonActionPerformed
        addLogDialog.setVisible(true);
    }//GEN-LAST:event_addLogButtonActionPerformed

    private void editLogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editLogButtonActionPerformed
        if (logList.getSelectedIndex() != -1) {
            String log = (String) logListModel.get(logList.getSelectedIndex());
            editLogDialog.setLog(log);
            editLogDialog.setVisible(true);
        }
    }//GEN-LAST:event_editLogButtonActionPerformed

    private void removeLogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeLogButtonActionPerformed
        if (logList.getSelectedIndex() != -1) {
            String log = (String) logListModel.get(logList.getSelectedIndex());
            //logListModel.removeElement(log);
            //need a warning and delete all connected events!
        }
    }//GEN-LAST:event_removeLogButtonActionPerformed

    private void runEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runEventButtonActionPerformed
        if (eventList.getSelectedIndex() != 1) {
            String out = core.runEvent(eventList.getSelectedIndex());
            jTextArea1.append(out);
        }
    }//GEN-LAST:event_runEventButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    	if (args.length == 0) {
    	
	        java.awt.EventQueue.invokeLater(new Runnable() {
	
	            public void run() {
	                new MainFrame().setVisible(true);
	            }
	        });
    	}
    	else {
    		core = new Core();
    		
    		core.readEventList();
    		core.readSeverList();
    		
    		if (args[0].equals("list")) {
    			ArrayList<String> eventList = core.getEvents();
    			System.out.println(eventList.size());
    			for(String event: eventList) {
    				System.out.println(event);
    			}
    		}
    	}
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adServerButton;
    private javax.swing.JButton addEventButton;
    private javax.swing.JButton addLogButton;
    private javax.swing.JLabel commandLabel;
    private javax.swing.JTextField commandTextField;
    private javax.swing.JButton editEventButton;
    private javax.swing.JButton editLogButton;
    private javax.swing.JList eventList;
    private javax.swing.JLabel eventsLabel;
    private javax.swing.JPanel eventsPanel;
    private javax.swing.JLabel ipLabel;
    private javax.swing.JTextField ipTextField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JList logList;
    private javax.swing.JLabel logListLabel;
    private javax.swing.JTabbedPane mainTabPanel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel nameLabel1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton removeEventButton;
    private javax.swing.JButton removeLogButton;
    private javax.swing.JButton removeServerButton;
    private javax.swing.JButton runEventButton;
    private javax.swing.JButton saveChangesButton;
    private javax.swing.JList serverList;
    private javax.swing.JLabel serverListLabel;
    private javax.swing.JTextField serverNameTextField;
    private javax.swing.JLabel serversLabel;
    private javax.swing.JPanel serversPanel;
    private javax.swing.JTextField serversTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the core
     */
    public Core getCore() {
        return core;
    }

    /**
     * @param core the core to set
     */
    public void setCore(Core core) {
        this.core = core;
    }

    /**
     * @return the eventListModel
     */
    public DefaultListModel getEventListModel() {
        return eventListModel;
    }

    /**
     * @param eventListModel the eventListModel to set
     */
    public void setEventListModel(DefaultListModel eventListModel) {
        this.eventListModel = eventListModel;
    }

    /**
     * @return the serverListModel
     */
    public DefaultListModel getServerListModel() {
        return serverListModel;
    }

    /**
     * @param serverListModel the serverListModel to set
     */
    public void setServerListModel(DefaultListModel serverListModel) {
        this.serverListModel = serverListModel;
    }

    /**
     * @return the logListModel
     */
    public DefaultListModel getLogListModel() {
        return logListModel;
    }

    /**
     * @param logListModel the logListModel to set
     */
    public void setLogListModel(DefaultListModel logListModel) {
        this.logListModel = logListModel;
    }
}
